<?php

namespace App\Services;

use App\Models\Course;
use App\Models\CourseMember;

class CourseService
{
    // 判断账号是否存在 并返回user_id
    public static function checkCourseRepeat($course){

        $start_time = $course['start_time'];
        $end_time = $start_time + $course['len'] * 60;

        $where = [
            ['start_time', '<=', $end_time],
            ['end_time', '>=', $start_time],
        ];

        if($course['class_id'] > 0){
            $where['class_id'] = $course['class_id'];
            $c = Course::where($where)->first();
            if($c){
                $course['conflict'] = 'class';
            }
        }

        if($course['teacher_id'] > 0){
            $where['teacher_id'] = $course['teacher_id'];
            $c = Course::where($where)->first();
            if($c){
                $course['conflict'] = 'teacher';
            }
        }

        if($course['classroom_id'] > 0){
            $where['classroom_id'] = $course['classroom_id'];
            $c = Course::where($where)->first();
            if($c){
                $course['conflict'] = 'classroom';
            }
        }

        return $course;
    }

    // 排课表
    public static function Courselist($request){

        $limit = $request->limit ?? 10;
        $start_time = $request->start_time ?? 0;
        $end_time = $request->end_time ?? 0;
        $class_id = $request->class_id ?? 0;

        $where = [
            'school_id' => $request->school_id,
            'campus_id' => $request->campus_id
        ];

        if($start_time > 0 && $end_time > 0 && $end_time > $start_time){
            $where[] = ['start_time', '>=', $start_time];
            $where[] = ['start_time', '<=', $end_time];
        }

        if($class_id > 0){
            $where[] = ['class_id', '=', $class_id];
        }

        $res = Course::where($where)
        ->with('subject:id,subject_name')
        ->paginate($limit);

        foreach ($res as $item) {

            $item->append('subject_name');
            $item->makeHidden('subject');

            $start = $item->start_time;
		    $end  = $item->end_time;
            $now = time();

            if ($start > $now ){
                $item->status = 1;
            }

            if ($start < $now && $end >= $now) {
                $item->status = 2;
            }

            if ($end < $now) {
                $item->status = 3;
            }

        }

        return $res;
    }

    // 获取学员课程
    public static function StudentCourselist($request){
        $limit = $request->limit ?? 10;
        $courseId = CourseMember::where('student_id', $request->student_id)->pluck('course_id');

        $res = Course::whereIn('id', $courseId)
        ->with('subject:id,subject_name')
        ->paginate($limit);

        foreach ($res as $item) {

            $item->append('subject_name');
            $item->makeHidden('subject');

            $start = $item->start_time;
		    $end  = $item->end_time;
            $now = time();

            if ($start > $now ){
                $item->status = 1;
            }

            if ($start < $now && $end >= $now) {
                $item->status = 2;
            }

            if ($end < $now) {
                $item->status = 3;
            }

        }
        return $res;
    }
}
